package com.wzw.conference.service;


import static com.wzw.conference.constant.TimePoint.NETWORKING_END_START_TIME;

import com.wzw.conference.constant.TimePoint;
import com.wzw.conference.entity.Session;
import com.wzw.conference.entity.SessionName;
import com.wzw.conference.entity.Talk;
import com.wzw.conference.entity.Track;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author wzw
 */
public class TrackService {

  private static final Map<String, Track> TRACKS = new ConcurrentHashMap<>();


  public void addMorningSessionToTrack(Track track) {
    Session morningSession = new Session(SessionName.MORNING_SESSION, TimePoint.MORNING_START_TIME,
        TimePoint.MORNING_START_TIME, TimePoint.LUNCH_START_TIME);
    track.addSession(morningSession);
  }

  public void addAfternoonSessionToTrack(Track track) {
    Session morningSession = new Session(SessionName.AFTERNOON_SESSION, TimePoint.AFTERNOON_START_TIME,
            TimePoint.AFTERNOON_START_TIME, NETWORKING_END_START_TIME);
    track.addSession(morningSession);
  }


  public Map<String, Track> getTracks() {
    return TRACKS;
  }


  public void trackAllTalks(List<Talk> talks) {
    AtomicInteger trackNumber = new AtomicInteger(1);
    Track track = initTrack(trackNumber.get());
    TRACKS.put(String.valueOf(trackNumber), track);
    for (Talk talk : talks) {
      if (track.canAddTalk(talk)) {
        track.addTalk(talk);
      }else {
        track = initTrack(trackNumber.incrementAndGet());
        track.addTalk(talk);
        TRACKS.put(String.valueOf(trackNumber), track);
      }
    }

  }




  public Track initTrack(Integer trackNumber) {
    Track track = new Track(trackNumber);
    addMorningSessionToTrack(track);
    addAfternoonSessionToTrack(track);
    return track;
  }


}
